package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.work.domain.vo.WorkFillProjectReport;
import com.ruoyi.work.domain.vo.WorkFillReport;
import com.ruoyi.work.domain.vo.WorkFillVo;
import com.ruoyi.work.service.IWorkDeclarerService;
import com.ruoyi.work.service.IWorkFillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: MMDZ
 * @Desc: TODO 工时填报
 */
@RestController
@RequestMapping("/work/fill")
public class WorkFillController extends BaseController {

    @Autowired
    private IWorkFillService workFillService;
    @Autowired
    private IWorkDeclarerService workDeclarerService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 初始化填报数据（今天已填写的填报记录）
     *
     * @return
     */
    @GetMapping("/init")
    @PreAuthorize("@ss.hasPermi('work:fill:init')")
    public AjaxResult init() {
        return workFillService.init();
    }

    /**
     * 工时填报
     *
     * @param vo
     * @return
     */
    @RepeatSubmit(interval = 60 * 1000, message = "1分钟内不允许重复填报")
    @PostMapping
    @Log(title = "工时填报", businessType = BusinessType.INSERT)
    public AjaxResult add(@Valid @RequestBody WorkFillVo vo) {
        if (!workDeclarerService.selectExistDeclarer(vo.getUserId()))
            throw new ServiceException("抱歉，你不是项目填报人员，填报失败！请联系管理员开放权限>_<", HttpStatus.ERROR);
        return AjaxResult.success(workFillService.insertWorkFill(vo));
    }

    /**
     * 我的工时-工时累计（审核过的有效工时）
     *
     * @param report
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:fill:query')")
    @GetMapping("/initFill")
    public AjaxResult query(WorkFillReport report) {
        return AjaxResult.success(workFillService.initFill(report));
    }

    /**
     * 我的工时-累计项目工时（审核过的有效工时）
     *
     * @param report
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:fill:query')")
    @GetMapping("/initPro")
    public AjaxResult query(WorkFillProjectReport report) {
        return AjaxResult.success(workFillService.initPro(report));
    }

    /**
     * 我的工时-导出工时累计
     */
    @Log(title = "导出工时累计信息数据", businessType = BusinessType.EXPORT)
    @PostMapping("/exportFill")
    public void export(HttpServletResponse response, WorkFillReport report) {
        List<WorkFillReport> list = workFillService.initFill(report);
        ExcelUtil<WorkFillReport> util = new ExcelUtil<WorkFillReport>(WorkFillReport.class);
        util.exportExcel(response, list, "工时累计信息数据");
    }

    /**
     * 我的工时-导出项目工时
     */
    @Log(title = "导出项目工时信息数据", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPro")
    public void exportProject(HttpServletResponse response, WorkFillProjectReport report) {
        List<WorkFillProjectReport> list = workFillService.initPro(report);
        ExcelUtil<WorkFillProjectReport> util = new ExcelUtil<WorkFillProjectReport>(WorkFillProjectReport.class);
        util.exportExcel(response, list, "项目工时信息数据");
    }

    /**
     * APP退出时，删除 redis 的token，及用户信息
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:fill:init')")
    @GetMapping("/deltoken")
    public AjaxResult DelAppTokenByRedis() {
        String token = getLoginUser().getToken();
        logger.info("===============>>> APP退出时，删除 redis 的token，及用户信息 , token : " + token);
        redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + token);
        return AjaxResult.success();
    }
}
